
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "bl3361.h"
#include "comm.h"


static void transfer_float(unsigned char value[4])
{
    double *p = (double *)value;
    hexdump(value, 4, "");
    tc_log("%f", *p);
}

static void transfer_double(unsigned char value[8])
{
    double *p = (double *)value;
    hexdump(value, 8, "");
    tc_log("%lf", *p);
}

int main(int argc, char const *argv[])
{
    tc_log("********************");
    tc_log("new start @ %s %s", __DATE__, __TIME__);
    tc_log("********************");

    // if (argc != 4) {
    //     tc_log_err("input err");
    //     return -1;
    // }

    unsigned char msg[256];
    int msglen, i;

#if 0
    msglen = bl3361_setup_rfcfg(atof(argv[1]), atoi(argv[2]), atoi(argv[3]), msg, sizeof(msg));
    hexdump(msg, msglen, "msglen %d", msglen);

    unsigned char val32[4] = {0x16, 0x35, 0x8f, 0x42};
    transfer_float(val32);

    unsigned char val64[8] = {0x79, 0xd9, 0x5a, 0xeb, 0xa2, 0xe6, 0x51, 0x40};
    transfer_double(val64);

    msglen = bl3361_setup_rfcfg(305.0F, 1000, 35, msg, sizeof(msg));
    if (msglen < 0) 
        goto err;

    hexdump(msg, msglen, "msglen %d", msglen);
#else
    unsigned int base_freq, freq, step;

    base_freq = 305000U;
    step = 1000;

    for (i = 0; (freq = (base_freq + i * step)) < 340000u; ++i) {
        msglen = bl3361_setup_rfcfg(freq, 100, 20, msg, sizeof(msg));
        if (msglen < 0) 
            goto err;
        hexdump(msg, msglen, "msglen %d freq %f", msglen, freq);
    }

    base_freq = 430000U;

    for (i = 0; (freq = (base_freq + i * step)) < 440000U; ++i) {
        msglen = bl3361_setup_rfcfg(freq, 100, 20, msg, sizeof(msg));
        if (msglen < 0) 
            goto err;
        hexdump(msg, msglen, "msglen %d freq %f", msglen, freq);
    }

    base_freq = 315100U;
    step = 100;

    for (i = 0; (freq = (base_freq + i * step)) < 316000U; ++i) {
        // tc_log("%f, %d * %f = %f, %f, %f", base_freq, i, step, i * step, base_freq + i * step, freq);
        msglen = bl3361_setup_rfcfg(freq, 10, 20, msg, sizeof(msg));
        if (msglen < 0) 
            goto err;
        hexdump(msg, msglen, "msglen %d freq %f", msglen, freq);
    }

    base_freq = 433100U;

    for (i = 0; (freq = (base_freq + i * step)) < 434000U; ++i) {
        msglen = bl3361_setup_rfcfg(freq, 10, 20, msg, sizeof(msg));
        if (msglen < 0) 
            goto err;
        hexdump(msg, msglen, "msglen %d freq %f", msglen, freq);
    }
#endif
    tc_log("success exit");
    return 0;

err:
    tc_log_err("failed %d", msglen);
    return -1;
}

